<template>
  <div id="make_knowledgeGraph">
    <div v-if="roles === 'teacher'" class="one-btns">
      <div class="title">知识库详情</div>
      <!-- <div class="all-btn" @click="saveKnowledgeGraph()">保存知识图谱</div> -->
    </div>
    <!-- 树形图 -->
    <div class="content">
      <div class="graph-tree">
        <el-tree
          :data="treeData"
          show-checkbox
          node-key="id"
          default-expand-all
          :props="defaultProps"
          :expand-on-click-node="false"
        >
          <template #default="{ node, data }">
            <div class="custom-tree-node">
              <span>{{ node.label }}</span>
              <div>
                <el-button
                  style="color: #457aff"
                  type="primary"
                  link
                  @click.stop="append(data)"
                >
                  <el-icon :size="12" color="#457aff" style="margin-right: 4px"
                    ><CirclePlus
                  /></el-icon>
                  新增
                </el-button>
                <el-button
                  style="color: #457aff"
                  type="primary"
                  link
                  @click.stop="remove(node, data)"
                >
                  <el-icon :size="12" color="#457aff" style="margin-right: 4px"
                    ><Close
                  /></el-icon>
                  删除
                </el-button>
              </div>
            </div>
          </template>
        </el-tree>
      </div>
      <div class="graph-form" v-if="isAdd">
        <div class="node-name">
          <span>名称</span>
          <el-input v-model="nodeName" maxlength="20" show-word-limit />
        </div>
        <div class="node-knowledge">
          <span>知识点</span>
          <el-input
            v-model="nodeKnowledge"
            resize="none"
            type="textarea"
            maxlength="200"
            show-word-limit
          />
        </div>
        <el-button class="saveBtn">保存</el-button>
      </div>
      <!-- <div class="nograph-form" v-else>暂无展示</div> -->
    </div>
    <el-button
      type="primary"
      color="#155EEF"
      @click="testModel()"
      style="margin-top: 20px"
      >预览页面效果</el-button
    >
  </div>
</template>
    
<script>
import HomeChart2 from "@/components/HomeChart2.vue";
export default {
  components: {
    HomeChart2,
  },
  name: "ZhishiIndex",
  data() {
    return {
      roles: "",
      nodeName: "",
      nodeKnowledge: "",
      isAdd: true,
      defaultProps: {
        children: "children",
        label: "name",
      },
      treeData: [],
    };
  },
  created() {
    this.roles = JSON.parse(localStorage.getItem("userInfo")).roles[0];
    this.getList();
  },
  methods: {
    getList() {
      const url = "/dify/graph-relation";
      const params = {
        bookInfoId: 1, // 使用 params 传递查询参数
      };

      this.$axios
        .get(url, {
          params, // 将参数传递给 `params` 属性
        })
        .then((res) => {
          this.treeData = res.data.datas;
        });
    },
    append(data) {
      this.isAdd = true;
    },

    remove(node, data) {},
  },
};
</script>
  <style lang="scss" scoped>
#make_knowledgeGraph {
  width: 100%;
  background-color: #f3f5fa;
  padding: 20px 30px;
  box-sizing: border-box;
  overflow-x: hidden;
  .one-btns {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    .title {
      font-size: 18px;
      color: #596184;
    }
  }
  .content {
    height: 100%;
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    .graph-tree {
      padding: 30px;
      overflow-y: auto;
      width: calc(50% - 10px);
      height: 510px;
      background: #ffffff;
      border-radius: 10px;
      box-sizing: border-box;
      &::-webkit-scrollbar {
        width: 5px;
      }
      &::-webkit-scrollbar-thumb {
        background-color: #e8ecf7;
        border-radius: 4px;
      }
    }

    .nograph-form {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 50px 40px;
      width: calc(50% - 10px);
      height: 510px;
      background: #ffffff;
      border-radius: 10px;
      box-sizing: border-box;
    }

    .graph-form {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      padding: 50px 40px;
      width: calc(50% - 10px);
      height: 510px;
      background: #ffffff;
      border-radius: 10px;
      box-sizing: border-box;
      .node-name {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        width: 100%;
        margin-bottom: 20px;
        span {
          flex-shrink: 0;
          font-size: 14px;
          color: #596184;
          margin-bottom: 13px;
        }
      }
      .node-knowledge {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        width: 100%;
        margin-bottom: 20px;
        :deep(.el-textarea__inner) {
          height: 263px;
        }
        span {
          flex-shrink: 0;
          font-size: 14px;
          color: #596184;
          margin-bottom: 13px;
        }
      }
      .saveBtn {
        background-color: #155eef;
        color: #fff;
      }
    }
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;

      // 默认隐藏操作按钮
      > div {
        opacity: 0;
        transition: opacity 0.2s ease-in-out;
      }

      // 鼠标悬停时显示操作按钮
      &:hover > div {
        opacity: 1;
      }
    }
  }
  .all-btn {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 4px;
    height: 40px;
    line-height: 40px;
    background: #457aff;
    border-radius: 4px;
    font-size: 14px;
    color: #fff;
    padding: 0 18px;
    cursor: pointer;
  }
}
</style>